package com.feng.leetcode;

public class Seventh {
    public static void main(String[] args) {
        System.out.println(reverse(1534236469));
    }

    public static int reverse(int x) {
        boolean isPositive = true;
        if (!(x>=0)){
            isPositive = false;
        }


        x = Math.abs(x);
        StringBuilder s = new StringBuilder(x + "");
        s = s.reverse();

        char[] chars = s.toString().toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] != 0){
                s.substring(i);
                break;
            }
        }

        try {
            if (isPositive){
                return Integer.parseInt(s.toString());
            }else{
                return 0 - Integer.parseInt(s.toString());
            }
        } catch (NumberFormatException e) {
            return 0;
        }

    }
}
